package com.qymerge.manage.data.service.impl;

import com.qymerge.manage.base.dao.IProductDao;
import com.qymerge.manage.base.dao.IUserWXDao;
import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.bean.DataTableRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.utils.ReadExcelUtil;
import com.qymerge.manage.data.dao.IPdzDao;
import com.qymerge.manage.data.dto.PdzDto;
import com.qymerge.manage.data.dto.export.PdzExportDTO;
import com.qymerge.manage.data.ro.PdzRO;
import com.qymerge.manage.data.service.IPdzService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *<p>Title: PdzServiceImpl</p>
 *<p>Description: 普通定制</p>
 *
 *@Author cong.zhang
 *@Version 1.0.0
 *@Date 2020/12/19
 *@Time 18:18
 */
@Service
public class PdzServiceImpl implements IPdzService {

    @Autowired
    private IPdzDao pdzDao;

    @Autowired
    private IUserWXDao userWXDao;

    @Autowired
    private IProductDao productDao;

    /**
     * 查询
     *
     * @param ro
     * @param pageRequest
     * @return
     */
    @Override
    public DataTableRO list(PdzDto ro, PageRequest pageRequest) {
        List<PdzRO> list = pdzDao.list(ro, pageRequest);
        for (PdzRO pdzRO : list) {
            // 设置用户信息
            long userwxid = pdzRO.getUserwxid();
            if (userwxid > 0) {
                pdzRO.setUser_info(userWXDao.findOne(userwxid));
            }

            // 设置产品信息
            String productid = pdzRO.getPname();
            if (StringUtils.isNotBlank(productid)) {
                pdzRO.setProduct_info(productDao.findOne(Long.valueOf(productid)));
            }
        }
        DataTableRO dataTableRO = new DataTableRO();
        int allCount = pdzDao.total(ro);
        int pageSize = pageRequest.getSize();
        dataTableRO.setAll_count(allCount);
        dataTableRO.setList(list);
        dataTableRO.setCurrent_page(pageRequest.getPage());
        dataTableRO.setPage_size(pageSize);
        int allPage = (allCount % pageSize == 0 ? allCount / pageSize : allCount / pageSize + 1);
        dataTableRO.setAll_page(allPage);
        return dataTableRO;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public PdzRO detail(Long id) {
        PdzRO pdzRO = pdzDao.findOne(id);

        if (null != pdzRO) {
            // 设置用户信息
            long userwxid = pdzRO.getUserwxid();
            if (userwxid > 0) {
                pdzRO.setUser_info(userWXDao.findOne(userwxid));
            }

            // 设置产品信息
            String productid = pdzRO.getPname();
            if (StringUtils.isNotBlank(productid)) {
                pdzRO.setProduct_info(productDao.findOne(Long.valueOf(productid)));
            }
        }

        return pdzRO;
    }

    @Override
    public ActionResultRO save(PdzRO ro) {
        int rn = 0;
        if (ro.getId() == 0) {
            rn = pdzDao.install(ro);
        } else {
            rn = pdzDao.update(ro);
        }
        return new ActionResultRO(rn, ro.getId() == 0 ? "保存成功" : "修改成功", ro.getId() == 0 ? "保存失败" : "修改失败");
    }

    @Override
    public ActionResultRO delete(Long[] ids) {
        int rn = pdzDao.remove(Arrays.asList(ids));
        return new ActionResultRO(true, "删除" + rn +"条成功", "删除" + rn +"条失败");
    }

    @Override
    public List<PdzExportDTO> exportData(PdzDto ro) {
        List<PdzExportDTO> list = new ArrayList<PdzExportDTO>();
        PdzExportDTO dto = null;

        List<PdzRO> pdzROS = pdzDao.findAll(ro);
        if (CollectionUtils.isNotEmpty(pdzROS)) {
            for (PdzRO pdzRO : pdzROS) {
                dto = new PdzExportDTO();

                dto.setId(pdzRO.getId());
                dto.setPname(pdzRO.getPname());
                dto.setCj(pdzRO.getCj());
                dto.setInfo(pdzRO.getInfo());
                dto.setYdate(pdzRO.getYdate());
                dto.setYtime(pdzRO.getYtime());
                dto.setIshx(pdzRO.getIshx() == 1 ? "是" : "否");
                dto.setUserwxid(pdzRO.getUserwxid());
                dto.setHxcode(pdzRO.getHxcode());
                dto.setHxdate(pdzRO.getHxdate());
                dto.setCreatedate(pdzRO.getCreatedate());

                list.add(dto);
            }
        }

        return list;
    }

    @Override
    public ActionResultRO importData(MultipartFile multipartFile, int flag) {
        StringBuffer tip = new StringBuffer("导入结果：<br/>");
        ReadExcelUtil readExcelUtil = new ReadExcelUtil();
        List<String[][]> datas = null;
        try {
            datas = readExcelUtil.readByStream(multipartFile.getInputStream(), 2, multipartFile.getOriginalFilename());
            if (org.springframework.util.CollectionUtils.isEmpty(datas)) {
                return new ActionResultRO(false, "execl数据为空");
            }
            String[][] data = datas.get(0);

            long id= 0L;
            String pname= null;
            String cj= null;
            String info= null;
            String ydate= null;
            String ytime= null;
            long userwxid= 0L;
            int ishx = 0;
            String hxcode=null;
            String hxdate=null;
            String createdate=null;

            PdzRO pdzRO = null;
            int rowNum = 1;        // 记录当前行
            int successNum = 0;    // 记录新增或者更新记录数
            for (String[] row : data) {
                rowNum++;

                id = Long.valueOf(row[0]);
                pname= row[1];
                cj= row[2];
                info= row[3];
                ydate= row[4];
                ytime= row[5];
                userwxid= Long.valueOf(row[6]);

                if (StringUtils.isNotBlank(row[7])) {
                    ishx= Integer.valueOf(row[7]);
                }
                hxcode= row[8];
                hxdate= row[9];
                createdate= row[10];

                if (StringUtils.isEmpty(id + "")
                        || StringUtils.isEmpty(ydate)) {
                    tip.append("第").append(rowNum).append("行：部分数据为空，不予新增<br/>");
                    continue;
                }

                pdzRO = new PdzRO(id,pname,cj,info, ydate, ytime, userwxid, ishx, hxcode, hxdate, createdate);

                if (flag == 1) {
                    pdzDao.install(pdzRO);
                    successNum++;
                } else if (flag == 0) {
                    pdzDao.update(pdzRO);
                    successNum++;
                }

            }
            tip.append("成功"+ (flag == 1 ? "新增":"更新") +"记录：").append(successNum).append("条");
        } catch (IOException e) {
            e.printStackTrace();
            return new ActionResultRO(false, "导入失败");
        }

        return new ActionResultRO(true, tip.toString());
    }

    @Override
    public ActionResultRO updateIshx(long id, int ishx, String msg) {
        int rn = pdzDao.updateIshx(id, ishx);
        return new ActionResultRO(rn, msg + "成功", msg + "失败");
    }

}
